import derelict.sdl.sdl;
import Pixels;
import Point;


void dummyLine(SDL_Surface *surface, point pkt0, point pkt1, uint value){
	float dx, dy, y, m;

	if(pkt0.x > pkt1.x){
		int tmp; tmp = pkt0.x; pkt0.x = pkt1.x; pkt1.x = tmp;
		tmp = pkt0.y; pkt0.y = pkt1.y; pkt1.y = tmp;
	}
	dy = pkt1.y - pkt0.y;
	dx = pkt1.x - pkt0.x;
	if(dx == 0){
		for(int yy = pkt0.y ; yy <= pkt1.y; yy++)
			putPixel(surface, pkt0.x, yy, value);
		return;
	}
	assert(dx != 0);
	m = dy / dx;
	y = pkt0.y;
	for(int x = pkt0.x; x <= pkt1.x; x++){
		putPixel(surface, x, cast(int)(y + .5f), value);
		y += m;
	}
}

void midPointLine(SDL_Surface *surface, point pkt0, point pkt1, uint value){
	int dx, dy, incrE, incrNE, d, x, y;

	if(pkt1.x < pkt0.x){
		int tmp;
		tmp = pkt0.x; pkt0.x = pkt1.x; pkt1.x = tmp;
		tmp = pkt0.y; pkt0.y = pkt1.y; pkt1.y = tmp;
	}

	dx = pkt1.x - pkt0.x;
	dy = pkt1.y - pkt0.y;

	if((dy >= 0) && (dy <= dx)){
		d = dy * 2 - dx;
		incrE = dy * 2;
		incrNE = (dy - dx) * 2;
		x = pkt0.x;
		y = pkt0.y;
		putPixel(surface, x, y, value);
		while(x < pkt1.x){
			if(d <= 0){
				d += incrE;
				x++;
			}
			else{
				d += incrNE;
				x++;
				y++;
			}
			putPixel(surface, x, y, value);
		}
	}
	else if((dy < 0) && (-dy <= dx)){
		d = 2 * dy + dx;
		incrE = dy * 2;
		incrNE = (dy + dx) *2;
		x = pkt0.x;
		y = pkt0.y;
		putPixel(surface, x, y, value);
		while(x < pkt1.x){
			if(d <= 0){
				d += incrNE;
				x++;
				y--;
			}
			else{
				d += incrE;
				x++;
			}
			putPixel(surface, x, y, value);
		}
	}
	else if((dy > 0) && (dy > dx)){
		d = 2 * dx -dy;
		incrE = dx * 2;
		incrNE = (dx - dy) * 2;
		x = pkt0.x;
		y = pkt0.y;
		putPixel(surface, x, y, value);
		while(y < pkt1.y){
			if(d <= 0){
				d += incrE;
				y++;
			}
			else{
				d += incrNE;
				y++;
				x++;
			}
			putPixel(surface, x, y, value);
		}
	}
	else if((dy < 0) && (-dy > dx)) {
		d = 2 * dx + dy;
		incrE = 2 * dx;
		incrNE = 2 * (dx + dy);
		x = pkt0.x;
		y = pkt0.y;
		putPixel(surface, x, y, value);
		while(y > pkt1.y){
			if(d <= 0){
				d += incrE;
				y--;
			}
			else{
				d += incrNE;
				y--;
				x++;
			}
			putPixel(surface, x, y, value);
		}
	}
}

alias midPointLine line;
